#include "interface.h"
#include <QDebug>
#include <QIcon>
#include <QApplication>
#include <QDesktopWidget>

Interface::Interface()
{
}

QPixmap Interface::loadSvg(const QString &fileName, const int size)
{
    QPixmap pixmap(size, size);
    QSvgRenderer renderer(fileName);
    pixmap.fill(Qt::transparent);

    QPainter painter;
    painter.begin(&pixmap);
    renderer.render(&painter);
    painter.end();

    return pixmap;
}

QPixmap Interface::drawSymbolicColoredPixmap(const QPixmap &source)//反白
{
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if (qAbs(color.red())<=100 && qAbs(color.green())<=100 && qAbs(color.blue())<=100) {
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

QPixmap Interface::drawSymbolicBlackColoredPixmap(const QPixmap &source)//反黑
{
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if (qAbs(color.red())>=200 && qAbs(color.green())>=200 && qAbs(color.blue())>=200) {
                    color.setRed(56);
                    color.setGreen(56);
                    color.setBlue(56);
                    img.setPixelColor(x, y, color);
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

void Interface::setFontSize(QLabel *label)
{
    if(QGSettings::isSchemaInstalled(QString("org.mate.interface").toLocal8Bit()))
    {
        QGSettings gsetting(QString("org.mate.interface").toLocal8Bit());
        if(gsetting.keys().contains("fontName"))
        {
            QString fontName=gsetting.get("font-name").toString();
//            qDebug()<<"the fontname is "<<fontName;
            QStringList valstr=fontName.split(" ");
//            qDebug()<<valstr<<"is the valstr";
            double fontSize=valstr.at(valstr.count()-1).toDouble();
//            qDebug()<<"the fontsize is "<<fontSize;
            QFont font;
//            font.setBold(true);
            font.setWeight(QFont::Medium);
            font.setPointSize(fontSize+2);
            label->setFont(font);
        }
    }
}

QPixmap Interface::getThemeLoadingIcon(const int number)
{
    QString img=QString("ukui-loading-%1-symbolic")
            .arg(number);
    auto desktopWidget = QApplication::desktop();
    const auto ratio=desktopWidget->devicePixelRatioF();
    QPixmap pixmap(QIcon::fromTheme(img).pixmap(QSize(16*ratio,16*ratio)));
    if(QGSettings::isSchemaInstalled(QString("org.ukui.style").toLocal8Bit()))
    {
        QGSettings gsetting(QString("org.ukui.style").toLocal8Bit());
        if(gsetting.keys().contains("styleName"))
        {
            if(gsetting.get("style-name").toString()=="ukui-dark")//反白
                pixmap=drawSymbolicColoredPixmap(pixmap);
        }

    }
    return pixmap;
}
